<?php

declare(strict_types=1);

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rules\Password;

class UpdateProfileRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
     */
    public function rules(): array
    {
        $userId = $this->user()->id;
        
        return [
            'name' => 'sometimes|string|max:255|unique:users,name,' . $userId,
            'email' => 'sometimes|email|max:255|unique:users,email,' . $userId,
            'password' => [
                'sometimes',
                'nullable',
                'confirmed',
                Password::min(8)
                    ->letters()
                    ->mixedCase()
                    ->numbers()
                    ->symbols()
                    ->uncompromised(),
            ],
            'password_confirmation' => 'sometimes|nullable|same:password',
            'current_password' => 'sometimes|required_with:password|current_password',
        ];
    }

    /**
     * Get custom messages for validator errors.
     *
     * @return array<string, string>
     */
    public function messages(): array
    {
        return [
            'name.max' => '用户名不能超过255个字符',
            'name.unique' => '该用户名已被使用',
            'email.email' => '请输入有效的邮箱地址',
            'email.max' => '邮箱地址不能超过255个字符',
            'email.unique' => '该邮箱已被使用',
            'password.confirmed' => '两次输入的密码不一致',
            'password_confirmation.same' => '两次输入的密码不一致',
            'current_password.required_with' => '修改密码时必须输入当前密码',
            'current_password.current_password' => '当前密码不正确',
        ];
    }
} 